#
# example029.py
#
# Watermarking by using MSB
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

import sys
import time

import getopt
from numpy import int16
from scipy.io.wavfile import read
from scipy.io.wavfile import write
from scipy.spatial.distance import chebyshev
from scipy.spatial.distance import euclidean
from scipy.stats import pearsonr
from uoc.wave.comparasion.snr import snr
from uoc.wave.euclidean import euclidean_similitude
from uoc.wave.watermark.message import bitarrray
from uoc.wave.watermark.schemes import msb as WS
from uoc.wave.watermark.schemes.generic import detection_function
from uoc.wave.watermark.schemes.generic import embedding_function
from uoc.wave.watermark.schemes.generic import retrieval_function

#--------------------------------------------------------------
# Parameters of the watermarking
#--------------------------------------------------------------
M = "hello!" # secret message in the watermark, string

def example029(inputfile):
    print "example029.py"
    print
    print "WS: MSB Watermarking"
    print
    #--------------------------------------------------------------
    # Read an audio file
    #--------------------------------------------------------------
    (sample_rate, S) = read(inputfile)
    #--------------------------------------------------------------
    # Add the watermark
    #--------------------------------------------------------------
    start = time.clock()
    s = embedding_function(S, \
                           bitarrray(M), \
                           watermarking_sheme=WS.embedding_function, \
                           max_value=32767.0,
                           frame_size=4)
    print 'Time elapsed E = ', time.clock() - start, 's'
    write("../example029.E.wav", sample_rate, s.astype(int16))
    #--------------------------------------------------------------
    # Identify the message
    #--------------------------------------------------------------
    start = time.clock()
    m = retrieval_function(s, \
                           watermarking_sheme=WS.retrieval_function, \
                           max_value=32767.0,
                           frame_size=4)
    print 'Time elapsed R = ', time.clock() - start, 's'
    #--------------------------------------------------------------
    # Detection
    #--------------------------------------------------------------
    start = time.clock()
    d = detection_function(s, \
                           bitarrray(M), \
                           watermarking_sheme=WS.detection_function, \
                           frame_size=4, \
                           message=m)
    print 'Time elapsed D = ', time.clock() - start, 's [', d, ']'
    #--------------------------------------------------------------
    # Transparency Evaluation: SNR
    #--------------------------------------------------------------
    start = time.clock()
    snr_value = snr(S, s)
    print 'Time elapsed SNR = ', time.clock() - start, 's, [', snr_value, 'dB ]'
    #--------------------------------------------------------------
    # Transparency Evaluation: Chebyshev distance
    #--------------------------------------------------------------
    start = time.clock()
    chebyshev_distance = chebyshev(S, s)
    print 'Time elapsed Chebyshev distance = ', time.clock() - start, 's [', chebyshev_distance, ']'
    #--------------------------------------------------------------
    # Transparency Evaluation: Euclidian distance
    #--------------------------------------------------------------
    start = time.clock()
    euclidean_D = euclidean(S, s)
    print 'Time elapsed Euclidean distance = ', time.clock() - start, 's, [', euclidean_D, ']'
    #--------------------------------------------------------------
    # Transparency Evaluation: Euclidian similitude
    #--------------------------------------------------------------
    start = time.clock()
    euclidean_S = euclidean_similitude(S, s)
    print 'Time elapsed Euclidian similitude = ', time.clock() - start, 's, [', euclidean_S, ']'
    #--------------------------------------------------------------
    # Transparency Evaluation: Pearson Coeff
    #--------------------------------------------------------------
    start = time.clock()
    pearson_coeff = pearsonr(S, s)
    print 'Time elapsed Pearson Coeff = ', time.clock() - start, 's [', pearson_coeff, ']'

def main(argv):
    inputfile = '../../../media/OSR_uk_000_0020_8k.wav'
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile="])
    except getopt.GetoptError:
        print 'example029.py -i <inputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'example029.py -i <inputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
    example029(inputfile)

if __name__ == "__main__":
    main(sys.argv[1:])
    print "Done"